package libaryinterface;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.SQLException;

import jdbc.*;
/**
 * Created with IntelliJ IDEA
 * Description:
 * User: YKH
 * Date: 2022-05-18
 * Time: 15:03
 * author:AddInterface
 */
public class AddInterface extends  JFrame{
    public AddInterface(){
        //设置窗口
        super("添加界面");
        Container container = this.getContentPane();

        //设置标签字体
        Font font = new Font("黑体", Font.BOLD, 20);

        //创建标签
        JLabel label1 = new JLabel("SNID:");
        label1.setFont(font );
        label1.setBounds(150,100,100,40);

        JLabel label2 = new JLabel("书名");
        label2.setFont(font );
        label2.setBounds(150,150,100,40);

        JLabel label3 = new JLabel("作者:");
        label3.setFont(font );
        label3.setBounds(150,200,100,40);

        JLabel label4 = new JLabel("出版社");
        label4.setFont(font );
        label4.setBounds(150,250,100,40);

        container.add(label1);
        container.add(label2);
        container.add(label3);
        container.add(label4);

        //创建文本框
        JTextField textField1 = new JTextField();
        textField1.setBounds(250,100,300,40);
        textField1.setFont(font);

        JTextField textField2 = new JTextField();
        textField2.setBounds(250,150,300,40);
        textField2.setFont(font);

        JTextField textField3 = new JTextField();
        textField3.setBounds(250,200,300,40);
        textField3.setFont(font);

        JTextField textField4 = new JTextField();
        textField4.setBounds(250,250,300,40);
        textField4.setFont(font);

        container.add(textField1);
        container.add(textField2);
        container.add(textField3);
        container.add(textField4);

        //创建按钮
        JButton button = new JButton("添加");
        button.setBounds(350,450,80,30);
        button.setFont(font);

        //设置按钮监听
        button.addActionListener(new ActionListener() {
            boolean flag;
            @Override
            public void actionPerformed(ActionEvent e) {
                String snid = textField1.getText();
                String name = textField2.getText();
                String author = textField3.getText();
                String press = textField4.getText();

                if( snid.equals("") && name.equals("") && author.equals("") &&press.equals("")) {
                    new MyDialogDemo("添加失败");
                } else {
                    try {
                        Insertjdbc insertjdbc = new Insertjdbc();
                        flag = insertjdbc.Insert(snid, name, author, press);
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }

                    //弹窗
                    if (flag) {
                        new MyDialogDemo("添加成功");
                    } else {
                        new MyDialogDemo("添加失败");
                    }
                }
            }
        });

        //获得容器
        container.setBackground(new Color(50, 144, 236));
        setBounds(200,200,800,600);
        setLayout(null);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        //添加
        container.add(button);
    }
}
